﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataManagement.Interfaces;
using Microsoft.SqlServer.Management.Smo;
using ICeFramework.DataManagement.Collections;

namespace ICeFramework.DataManagement.MSSql
{
    public class MSSqlTable : ITable
    {
        private Table _table = null;        

        #region public MSSqlTable(Table table)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlTable</b> class.
        /// </summary>
        /// <param name="table"></param>
        public MSSqlTable(Table table)
        {
            _name = table.Name;
            _table = table;
            //foreach (Column col in table.Columns)
            //{
            //    IColumn msSqlCol = new MSSqlColumn(this, col);
            //    _columns.Add(msSqlCol);
            //}
            //foreach (ForeignKey fk in table.ForeignKeys)
            //{
            //    IForeignKey msSqlFK = new MSSqlForeignKey(this, fk);
            //    _foreignKeys.Add(msSqlFK);
            //}            
        }
        #endregion

        public void GetColumns()
        {
            _columns.Clear();
            foreach (Column col in _table.Columns)
            {
                IColumn msSqlCol = new MSSqlColumn(this, col);
                _columns.Add(msSqlCol);
            }
        }

        public void GetForeignKeys()
        {
            _foreignKeys.Clear();
            foreach (ForeignKey fk in _table.ForeignKeys)
            {
                IForeignKey msSqlFK = new MSSqlForeignKey(this, fk);
                _foreignKeys.Add(msSqlFK);
            }            
        }

        #region public override string ToString()
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlTable"/>.
        /// </summary>
        /// <returns>A <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlTable"/>.</returns>
        public override string ToString()
        {
            return _name;
        }
        #endregion

        private string _name;
        #region public string Name
        /// <summary>
        /// Get/Sets the Name of the MSSqlTable
        /// </summary>
        /// <value></value>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        #endregion

        private SqlColumnList _columns = new SqlColumnList();
        #region public SqlColumnList Columns
        /// <summary>
        /// Get/Sets the Columns of the MSSqlTable
        /// </summary>
        /// <value></value>
        public SqlColumnList Columns
        {
            get
            {
                return _columns;
            }
            set
            {
                _columns = value;
            }
        }
        #endregion

        private SqlForeignKeyList _foreignKeys = new SqlForeignKeyList();
        #region public SqlForeignKeyList ForeignKeys
        /// <summary>
        /// Get/Sets the ForeignKeys of the MSSqlTable
        /// </summary>
        /// <value></value>
        public SqlForeignKeyList ForeignKeys
        {
            get
            {
                return _foreignKeys;
            }
            set
            {
                _foreignKeys = value; ;
            }
        }
        #endregion

    }
}
